<!-- 真空设备选择器 -->
<template>
  <div>
    <el-select v-model="currentDeviceId" placeholder="请选择设备" @change="deviceIdChange" clearable>
          <el-option
            v-for="item in deviceList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
  </div>
</template>

<script setup lang="ts">
import { VacuumDeviceApi } from '@/api/vs/vacuumdevice'

const props = defineProps({
  deviceId: {
    type: Number,
    default: null
  }
})

watch(()=> props.deviceId,
() => {
  if(props.deviceId) {
    currentDeviceId.value = Number(props.deviceId)
  }
})

const emit = defineEmits(['update:deviceId'])

const deviceIdChange = () => {
  emit('update:deviceId', currentDeviceId.value)
}

onMounted(()=>{
  if(props.deviceId) {
    currentDeviceId.value = props.deviceId
  }
})

// 当前选中设备
const currentDeviceId = ref()

// 真空设备列表
const deviceList = ref([])

// 获取真空设备列表
const loadVacuumDeviceList = async() => {
  try {
    const res = await VacuumDeviceApi.getVacuumDeviceList()
    deviceList.value = res.map((item)=>{
      return {
        name: item.name,
        id: Number(item.id)
      }
    })
  } catch (error) {
    console.log(error)
  }
}

onMounted(loadVacuumDeviceList)
</script>

<style scoped>

</style>